%%%---------------------------------------------------------------------------------------
%%% @author     Stuart Jackson <simpleenigma@gmail.com> [http://erlsoft.org]
%%% @copyright  2006 - 2007 Simple Enigma, Inc. All Rights Reserved.
%%% @doc        Multi Homing module for INETS
%%% @reference  See <a href="http://erlsoft.org/modules/erlweb" target="_top">Erlang Software Framework</a> for more information
%%% @reference See <a href="http://erlweb.googlecode.com" target="_top">ErlWeb Google Code Repository</a> for more information
%%% @version    0.0.2
%%% @since      0.0.1
%%% @end
%%%
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2007 Stuart Jackson, Simple Enigma, Inc. All Righs Reserved
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%
%%%
%%%---------------------------------------------------------------------------------------
-module(mod_multi).
-author('sjackson@simpleenigma.com').
-include_lib("inets/src/httpd.hrl").
-include("../include/ews.hrl").

-export([do/1,load/2,store/2,remove/1]).

do(Mod) -> 
	Host = get_host(Mod),
	Port = get_port(Host),
	?D({Host,Port}),
	Headers = proplists:delete("host",Mod#mod.parsed_header),
	Method = list_to_atom(string:to_lower(Mod#mod.method)),
	URL = "http://127.0.0.1:" ++ integer_to_list(Port) ++ Mod#mod.request_uri,
	Response = case Method of
		get     -> http:request(get,{URL,Headers},[],[]);
		head    -> http:request(head,{URL,Headers},[],[]);
		put     -> http:request(put,{URL,Headers},[],[]);
		post    -> http:request(post,{URL,Headers},[],[]);
		trace   -> http:request(trace,{URL,Headers},[],[]);
		options -> http:request(options,{URL,Headers},[],[]);
		delete  -> http:request(delete,{URL,Headers},[],[]);
		_ -> ok
	end,
	case Response of
		{ok,{{_HTTPVersion,StatusCode,_ReasonPhrase},RespHeaders,RespBody}} -> ok
	end,
	CodeRespHeaders = [{code,StatusCode}|RespHeaders],
	?D(CodeRespHeaders),
	
	
	{proceed, [{response,{response,CodeRespHeaders,RespBody}}]}.

load(_Line,_Context) -> ok.
store({Key,Value},_List) -> {ok,{Key,Value}}.
remove(_ConfigDB) -> ok.


get_host(Mod) ->
	Headers = Mod#mod.parsed_header,
	proplists:get_value("host",Headers,[]).


get_port(HostName) -> proplists:get_value(HostName,find_inets(),0).
	
	
	
find_inets() ->
	Compare = "httpd_instance_sup__127_0_0_1__",
	{Instances,_Not} = lists:partition(fun(Element) ->
		String = atom_to_list(Element),
		ShortString = string:left(String,length(Compare)),
		if
			ShortString == Compare -> true;
			true -> false
		end
	end,registered()),
	Pids = lists:map(fun(I) -> whereis(I) end,Instances),
	Inets = lists:map(fun(P) -> [{pid,P} | httpd:info(P)] end,Pids),
	lists:map(fun(I) -> 
		ServerName = proplists:get_value(server_name,I),
		Port = proplists:get_value(port,I),
		{ServerName,Port}
	end,Inets).


























